export function sleep(ms: number): Promise<void> {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

export function isArrayBuffer(data: unknown): data is ArrayBuffer {
    return data instanceof ArrayBuffer;
}

export function bufferToString(buffer: Uint8Array): string {
    let str = "";
    let count = 0;

    for (const v of buffer) {
        count++;
        switch (count) {
            case 1:
                str += v.toString(16).padStart(2, "0");
                break;
            case 5:
                str += "  " + v.toString(16).padStart(2, "0");
                break;
            case 9:
                str += "   " + v.toString(16).padStart(2, "0");
                break;
            case 13:
                str += "  " + v.toString(16).padStart(2, "0");
                break;
            case 17:
                str += " " + v.toString(16).padStart(2, "0") + "\n";
                count = 0;
                break;
            default:
                str += " " + v.toString(16).padStart(2, "0");
                break;
        }
    }
    return str;
}

export function now(): string {
    return new Date().toLocaleTimeString();
}

export function hex(value: number): string {
    return value.toString(16).padStart(2, "0").toUpperCase();
}
